﻿Public Class ABGROUP
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection
    Dim PROVIDER_MASABOGROUP As New PROVIDERSCLASS

    Private Sub ABGROUP_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridabogroup()

        DesignForm(Me)
        DesignGrid(ABOGROUPGrid)

        PROVIDER_MASABOGROUP.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SelectDatagridabogroup()
        Dim objSelectDatamasabogroup As New masabogroup
        Dim MASABOGROUP As DataTable
        MASABOGROUP = connect.GetTable(objSelectDatamasabogroup.selectMasabogroup_)
        ABOGROUPGrid.PrimaryGrid.DataSource = MASABOGROUP
    End Sub

    Private Sub ABOGROUPGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles ABOGROUPGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสกรุ๊ปเลือด").Value() IsNot DBNull.Value Then
            abogroupID.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสกรุ๊ปเลือด").Value()
        Else
            abogroupID.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("กรุ๊ปเลือด").Value() IsNot DBNull.Value Then
            abogroupTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("กรุ๊ปเลือด").Value()
        Else
            abogroupTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            abogroupStatus.Checked = True
        Else
            abogroupStatus.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE masabogroup SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`status` = 1 "
                Else
                    Sql += "`status` = 0 "
                End If
                Sql += "WHERE `abogroup` = '" & abogroupID.Text & "' ;"

                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        connect.Dispose()
        'connectlocal = conlocal.NewConnection()
        'If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
        '    Try
        '        Dim Sql As String
        '        connectlocal.BeginTrans()
        '        Sql = "UPDATE ABOGROUP SET "
        '        If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
        '            Sql += "[STATUS] = 1 "
        '        Else
        '            Sql += "[STATUS] = 0 "
        '        End If
        '        Sql += "WHERE [ABOGROUP] = '" & abogroupID.Text & "' ;"

        '        connectlocal.ExecuteNonQuery(Sql)
        '        connectlocal.CommitTrans()
        '    Catch ex As Exception
        '        connectlocal.RollbackTrans()
        '        MsgBox(ex.ToString)
        '    End Try
        'End If
        'connectlocal.Dispose()
        addAbogroup.Enabled = False
    End Sub

    Private Sub addAbogroup_Click_1(sender As Object, e As EventArgs) Handles addAbogroup.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASABOGROUP.PADD_ = True Then
                Dim objInsertData As New masabogroup
                objInsertData.masabogroupDesc_ = abogroupTxt.Text
                objInsertData.masabogroupStatus_ = abogroupStatus.Checked
                objInsertData.insertAbogroupData()

                SelectDatagridabogroup()

                abogroupID.Text = ""
                abogroupTxt.Text = ""
                abogroupStatus.Checked = False
            Else
                MsgBox(PROVIDER_MASABOGROUP.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editAbogroup_Click_1(sender As Object, e As EventArgs) Handles editAbogroup.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASABOGROUP.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลกรุ๊ปเลือดดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                If editStat = 1 Then
                    Dim objEditData As New masabogroup
                    objEditData.masabogroupID_ = CInt(abogroupID.Text)
                    objEditData.masabogroupDesc_ = abogroupTxt.Text
                    objEditData.masabogroupStatus_ = abogroupStatus.Checked
                    objEditData.updateAbogroupData()

                    SelectDatagridabogroup()

                    abogroupID.Text = ""
                    abogroupTxt.Text = ""
                    abogroupStatus.Checked = False
                End If
                addAbogroup.Enabled = True
            Else
                MsgBox(PROVIDER_MASABOGROUP.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL(masabogroup.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL(masabogroup.Text)
        nextform.ShowDialog()
    End Sub
End Class